;; The first four lines of this file were added by Dracula.
;; They tell DrScheme that this is a Dracula Modular ACL2 program.
;; Leave these lines unchanged so that DrScheme can properly load this file.
#reader(planet "reader.rkt" ("cce" "dracula.plt") "modular" "lang")
(require "iimage.lisp")
(require "icommon.lisp")
;(require "ithreshold.lisp")
(require "icolorthreshold.lisp")

(module MColorThreshold
  (include-book "list-utilities" :dir :teachpacks)
  (include-book "io-utilities" :dir :teachpacks)
  (import IImage)
  (import ICommon)
;  (import IThreshold)

  ;function checks if one pixel is less than the threshold passed by user
  ;copied from mThreshold to avoid linking problems with Mmain
  (defun pixelLessThanColorThreshold (pixel threshold)
    (if (<= (car pixel) (car threshold))
       (if (<= (cadr pixel) (cadr threshold))
            (if (<= (caddr pixel)(caddr threshold))
                t
                nil)
            nil)
        nil))

  ;Replaces all pixels that are less than the threshold value with a 
  ;new RGB color value.
  ;pre: tree - the bitmap tree
  ;     threshholdValue - an RGB list representing the threshold color
  ;     newColor - an RGB list representing the new color to change all 
  ;                pixels at or below the threshold color
  ;     x - the column of the pixel
  ;     y - the row of the pixel
  ;     treeWidth - integer width of the bitmap
  ;     treeHeight - integer height of the bitmap
  ;post: the bitmap tree with all pixels at or below the threshold color 
  ;      changed to the new color
  ;Base lines: 11 
  ;Modified: 2
  (defun processColorThreshold (tree thresholdValue newColor x y treeWidth treeHeight)
    (let* ((nextX (getNextXCoordinate x treeWidth))
           (nextY (getNextYCoordinate x y treeWidth))
           (pixel (getPixel tree x y))
           (newR (car newColor))
           (newG (cadr newColor))
           (newB (caddr newColor))
           (newPixelVal (if (pixelLessThanColorThreshold pixel thresholdValue) 
                            (createRGB newR newG newB) pixel))
           (newPixRVal (car newPixelVal))
           (newPixGVal (cadr newPixelVal))
           (newPixBVal (caddr newPixelVal))) 
      (if (isValidPixel nextX nextY treeWidth treeHeight)
          (processColorThreshold (insertPixel tree x y newPixRVal newPixGVal newPixBVal) 
                                 thresholdValue newColor nextX nextY treeWidth treeHeight)
          (insertPixel tree x y newPixRVal newPixGVal newPixBVal))))


  ; Uses processColorThreshold to change all pixels in a given bitmap with RGB values
  ; below a given threshold color to a new color.
  ; pre: tree - the bitmap tree representation
  ;      threshColor - an RGB list representing the threshold color
  ;      newColor - an RGB value representing the new color to change all pixels below
  ;                 the threshold to
  ; post: a bitmap tree with all pixels below the given threshold color changed to the
  ;       new color
  ; Base: 12
  ; Modified: 6
  ; Added: 9
  (defun MainColorThreshold (tree args)
    (let* ((tR (getElement args 1)) ;threshold R
           (tG (getElement args 2)) ;threshold G
           (tB (getElement args 3)) ;threshold B 
           (nR (getElement args 4)) ;new color R
           (nG (getElement args 5)) ;new color G
           (nB (getElement args 6)) ;new color B
           (thresholdValue (createRGB tR tG tB))
           (threshWithinRange (validPixel thresholdValue))
           (newColorValue (createRGB nR nG nB)) 
           (newColorWithinRange (validPixel newColorValue))
           (threshError '("The threshold presented is not valid"))
           (newColorError '("The color to change all pixels within threshold is not valid"))
           (treeWidth (getWidth tree))
           (treeHeight (getHeight tree)))
      (if (and threshWithinRange newColorWithinRange) 
          (mv (processColorThreshold tree thresholdValue newColorValue 
                                     1 treeHeight treeWidth treeHeight) 
              (list "Successfully applied color threshold to image."))
          (if (threshWithinRange)
              (mv tree newColorError)
              (mv tree threshError)))))

  (export IColorThreshold))